﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Threading;

using netEntity = tuReserva.Model;
using tuReserva.Service;

namespace tuReserva.com.Controllers
{
    public class LoginController : Controller
    {
		private ApplicationService _appService;

		public LoginController()
		{
			this._appService = new ApplicationService();
		}

        public ActionResult Index()
        {
            return View();
        }

		public ActionResult Login(string userName, string password)
		{
			netEntity.User u = new netEntity.User();
			u.UserName = userName;
			u.Password = password;

			if (this.ValidateUser(u))
			{
				Session["User"] = this._appService.getCurrentUser();

				if (this._appService.getCurrentUser().Type.Equals(netEntity.User.OWNER))
				{
					return RedirectToAction("Index", "Owner");
				}
				return RedirectToAction("Index", "Customer");
			}

			return View("AccessDenied");//new JsonResult() { Data = "Usuario invalido", JsonRequestBehavior = JsonRequestBehavior.AllowGet };
		}

		private bool ValidateUser(netEntity.User u)
		{
			return this._appService.Login(u);
		}
    }
}
